//app.js
var config = require('./config.js');
var utils = require('./utils/util')
App({
  data: {

  },
   onShow: function (options) {
    let that = this;

   

    console.log(options);
    let q = decodeURIComponent(options.query.q);
    q = q.substring(36);
   
 
    if(q.length == 0){
      that.globalData.channel='all';
    }else{
    
      that.globalData.channel=q;
    }
    if(q == "21S791SO88NUPK"){

      that.globalData.hero=3;
    }else if(q == "21SB91SOOHUUPK"){
      that.globalData.hero=3;

    }else if(q == "21S491C8811EP4"){
      that.globalData.hero=3;

    }else if(q == "21S191COOHHUPK"){
      that.globalData.hero=3;

    }else if(q == "21S591C8OHHEPK"){
      that.globalData.hero=4;

    }else if(q == "21S291COO11UP4"){
      that.globalData.hero=4;

    }else if(q == "21S691C8O11EP4"){
      that.globalData.hero=1;
    }else if(q == "21S891SO811UP4"){
      that.globalData.hero=2;
    }else if(q == "21SA91SOO1UUP4"){
      that.globalData.hero=2;

    }else if(q == "21S391C888NEPK"){
      that.globalData.hero=2;

    }else if(q == "21S091CO811UP4"){
      that.globalData.hero=2;

    }else{
      that.globalData.hero=0;
    }

    // wx.showModal({
    //   title: '提示',
    //   content:that.globalData.channel,
    //   success: function(res) {
        
    //   }
    // })
    wx.getSystemInfo({
      success: function (res) {
        if (res.model.indexOf("iPhone X") > -1) {
          that.globalData.isIpx = true;
        }
      }
    });

  },
  onLaunch: function (options) {


  },getUserInfo: function(cb) {
 
		var that = this
		if(this.globalData.userInfo) {
		
			typeof cb == "function" && cb(this.globalData.userInfo)
		} else {
			//调用登录接口
			wx.login({
				success: function(res) {
				 console.log(res.code);
				 
					if(res.code) {
            var code = res.code;
            var watermark = {"appid": "wxa3c90f1b5eb8df05"}
            var data = {
              "code": code,
              "scope": "base",
              "watermark": watermark,
            };
  
            wx.request({
              url: "https://staging-oauth.usocialplus.com/57bd51aa5b5529760f8b4567/v2/weapp/oauth",
              data: data,
              method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              header: {
                'content-type': 'application/json'
              }, // 设置请求的 header
              success: function (res) {
                console.log(res);
                that.globalData.weappuserInfo = res.data;
                console.log(that.globalData.weappuserInfo);

                that.getAvenue();

              // 1.加载活动首页 标签
              var trackingdata = {
                "member_ids": ["58a56c887c423400da0dca73"],
                "tags": ["PRO_5_1_1", "INT_1_1_1", "ACT_3_3"]
              } 
              that.addTracking(trackingdata);


              // 1.加载活动首页 事件
              var trackingdata = {
                "accountId": "57bd51aa5b5529760f8b4567",
                "memberId": "5575548613747370538b4571",
                "msgType": "customerEvent",
                "subType": "c_Page_Loading",
                "createTime": 1529647042000,
                "eventProperties": {
                    "Source_Channel": "https://clear-marvel.ulcampaign.com/21S791SO88NUPK",
                    "Ref_ID": "5575548613747370538b4571",
                    "Campaign_ID": "dts_campaign_clear_2018_marvel"
                }
              }
              that.addTracking(trackingdata);

                
              },
              fail: function (err) {
                console.log(err);
              }
            }) 
					 
					} else {
						console.log('获取用户登录态失败！' + res.errMsg)
					}
				},fail: function(res) {
					console.log(res.code);
					
				}
				,complete: function(res) {
					// console.log(res.code);
					// console.log(res);
				}
			});
		}
		 
	},getAvenue: function () {
    var that = this;
    var weappuserInfo = that.globalData.weappuserInfo;
    console.log(weappuserInfo);
    wx.request({
      url: config.POST_URL+'index.php/Home/Index/avenue',
      data: {
        'miniopenid': weappuserInfo.openId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
       
          that.globalData.avenue=res.data.code;
          console.log(that.globalData.avenue);
      
          
      },
      fail: function (err) {
        console.log(err);
      }
    })
     
  },addTracking: function (data){

   
    wx.request({
      url: config.POST_URL+'index.php/Home/Index/addTracking',
      data: data,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function (res) {
           console.log(res);
      },
      fail: function (err) {
        console.log(err);
      }
    })
  },
  globalData: {
    userInfo: null,
    weappuserInfo:null,
    isIpx: false,
    phone:null,
    channel:null,
    hero:0,
    avenue:null,
  }
})